package com.ulook.lookplanet.ui.square.article;

import android.graphics.Bitmap;
import android.widget.TextView;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.ulook.lookplanet.R;
import com.ulook.lookplanet.base.BaseViewModel2;
import com.ulook.lookplanet.common.AppServer;
import com.ulook.lookplanet.data.model.TArtPic;
import com.ulook.lookplanet.data.model.TComment;
import com.ulook.lookplanet.data.model.TTopic;
import com.ulook.lookplanet.data.repository.ArticleRepository;
import com.ulook.lookplanet.ui.square.VArticlePreInfo;
import com.ulook.lookplanet.util.ThrowException;
import com.ulook.lookplanet.util.json.JacksonUtil;
import com.ulook.lookplanet.util.net.AsynRequestUtil;
import com.ulook.lookplanet.util.net.OkHttpException;
import com.ulook.lookplanet.util.net.RequestParams;
import com.ulook.lookplanet.util.net.ResponseCallback;

import java.util.List;

/**
 * 描述：
 *
 * @author L1998
 * @data 2021/4/21
 */
public class ArticleViewModel extends BaseViewModel2 {

    private ArticleRepository repository;
    private int currUserId;
    private VArticlePreInfo articlePreInfo;
    private MutableLiveData<List<TArtPic>> images = new MutableLiveData<>();
    private MutableLiveData<List<TTopic>> topics = new MutableLiveData<>();
    private MutableLiveData<VArtCount> vArtCount = new MutableLiveData<>();
    private MutableLiveData<VCommentInfo> myComment = new MutableLiveData<>();
    private MutableLiveData<List<VCommentInfo>> commentInfos = new MutableLiveData<>();
    private MutableLiveData<CommentState> state = new MutableLiveData<>();

    public ArticleViewModel(){
        repository = ArticleRepository.getInstance();
    }

    public void setCurrUserId(int currUserId){
        this.currUserId = currUserId;
    }
    public void setArticlePreInfo(VArticlePreInfo articlePreInfo){
        this.articlePreInfo = articlePreInfo;
    }
    public int getCurrUserId() {
        return currUserId;
    }
    public VArticlePreInfo getArticlePreInfo() {
        return articlePreInfo;
    }

    public LiveData<List<TArtPic>> getImages(){
        return images;
    }
    public LiveData<List<TTopic>> getTopics(){
        return topics;
    }
    public LiveData<VArtCount> getArtCount(){
        return vArtCount;
    }
    public LiveData<VCommentInfo> getMyComment(){
        return myComment;
    }
    public LiveData<List<VCommentInfo>> getCommentInfos(){
        return commentInfos;
    }
    public LiveData<CommentState> getState(){
        return state;
    }

    /**
     * 获取话题
     */
    public void getArtTopics(String artId){
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                try {
                    List<TTopic> artPicList = JacksonUtil.json2list((String)res, TTopic.class);
                    topics.setValue(artPicList);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(OkHttpException failuer) { }
        };
        RequestParams params = new RequestParams();
        params.put("artId", articlePreInfo.getUid());
        AsynRequestUtil.post(AppServer.ROOT_PATH + "/article/getTopicByComId", params, null, callback);
    }

    /**
     * 获取图片
     */
    public void getPic(String artId){
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                try {
                    List<TArtPic> artPicList = JacksonUtil.json2list((String)res, TArtPic.class);
                    images.setValue(artPicList);
                } catch (Exception e) {
                    ThrowException.print(e);
                }
            }
            @Override
            public void onFailure(OkHttpException failuer) {
                ThrowException.print(failuer, failuer.getEmsg());
            }
        };
        repository.getPic(artId, callback);
    }

    /**
     * 获取统计数据
     */
    public void getCount(String artId){
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                VArtCount artCount = (VArtCount) res;
                vArtCount.setValue(artCount);
            }
            @Override
            public void onFailure(OkHttpException failuer) {
                ThrowException.print(failuer, "查询统计数据失败");
            }
        };
        repository.getCount(artId, callback);
    }

    /**
     * 评论
     */
    public void commentAction(String content, VCommentInfo commentInfo){
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                myComment.setValue((VCommentInfo) res);
                setToastText("评论成功");
            }
            @Override
            public void onFailure(OkHttpException failuer) {
                setToastText("评论失败");
            }
        };
        TComment comment = new TComment();
        comment.setArtid(articlePreInfo.getUid());
        comment.setUserid(currUserId);
        comment.setContent(content);
        if(commentInfo != null){
            comment.setUpid(commentInfo.getUpid());
            comment.setReplyuser(commentInfo.getUserid());
        }
        repository.commentAction(comment, callback);
    }

    /**
     * 获取评论数据
     */
    public void getComment(int itemNum){
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                try {
                    List<VCommentInfo> commentInfoList = JacksonUtil.json2list((String)res, VCommentInfo.class);
                    if(commentInfoList.size() > 0){
                        commentInfos.setValue(commentInfoList);
                        state.setValue(new CommentState(1));
                    }else {
                        state.setValue(new CommentState(true));
                    }
                } catch (Exception e) {
                    state.setValue(new CommentState(-1));
                    setToastText("获取品论失败");
                    ThrowException.print(e);
                }
            }
            @Override
            public void onFailure(OkHttpException failuer) {
                state.setValue(new CommentState(-1));
                setToastText("获取品论失败");
                ThrowException.print(failuer.getEmsg());
            }
        };
        state.setValue(new CommentState(0));
        repository.getComment(articlePreInfo.getUid(), itemNum, callback);
    }
}
